/**
 * Created by HEKI on 8/24/2016.
 */
angular.module("config")
    .controller("showFoodsController", function ($scope, $routeParams, $http) {
        var mId = $routeParams.mId;
        var cart = [];
        $scope.cart = cart

        var foods = [];
        $scope.foods = foods;

        //获取商家信息并渲染
        var getMerchantInfoUrl = {
            method: "GET",
            url: "/merchant/" + mId
        };
        $http(getMerchantInfoUrl)
            .then(function (res) {
                console.log(res.data);
                $scope.merchant = res.data;
            });


        //获取食物信息并渲染
        var getMerchantFoodUrl = {
            method: "GET",
            url: "/food?mId=" + mId
        }
        $http(getMerchantFoodUrl)
            .then(function (res) {
                console.log(res.data);
                foods = res.data;
                $scope.foods = res.data;
                // console.log()
            });

        //获取购物车信息并渲染
        var getFoodCartUrl = {
            method: "GET",
            url: "/cart?mId=" + mId
        }

        $http(getFoodCartUrl)
            .then(function (res) {
                if (res.data) {
                    for (var i in res.data) {
                        var cartItem = res.data[i];
                        cart.push(cartItem);
                    }
                }
            });

        $scope.addToCart = function (index) {
            var req = {
                method: "put",
                url: "/cart",
                data: {
                    foodCart: cart,
                    mId: mId
                },
                headers: {"Content-type": "application/json"}
            }
            $http(req)
                .then(function (res) {
                    if (res.data != "error") {
                        var t = -1;
                        var exits = cart.some(function (o, inx, ary) {
                            t = inx;
                            return angular.equals(o.item, foods[index]);
                        });
                        console.log(res.data);
                        if (!exits) {
                            var cartItem = {item: foods[index], num: 1};
                            cart.push(cartItem);
                        }
                        else {
                            cart[t].num += 1;
                        }
                    }
                    else {
                        alert("请先登录");
                    }
                });
        }


        $scope.$watch("cart", function (n, o) {
            /*console.log(n);//变化后的值new
             console.log(o);//变化前的值old*/
            $scope.sum = 0;
            for (c in cart) {
                var totalPrice = 0;
                totalPrice = cart[c].item.fPrice * cart[c].num;
                $scope.sum += totalPrice;
            }
            ;
        }, true);

        $scope.changeFoodsNum = function (index, val) {
            if (val > 0) {
                if (val > 100) {
                    // alert("请勿恶意操作");
                    cart[index].num = 100;
                }
            }
            else {
                console.log(cart);
                console.log("index:" + index)
                cart.splice(index, 1);
            }
            var req = {
                method: "put",
                url: "/cart",
                data: {
                    foodCart: cart,
                    mId: mId
                },
                headers: {"Content-type": "application/json"}
            }
            $http(req)
                .then(function (res) {
                    console.log(res.data);
                });
        }

        $scope.commitOrder = function () {
            var req = {
                method: "post",
                url: "/order",
                data: {
                    cId: $scope.client._id,
                    mId: mId,
                    foodInfo: $scope.cart,
                    merchant: $scope.merchant,
                    client: $scope.client
                },
                headers: {"Content-type": "application/json"}
            }
            $http(req)
                .then(function (res) {
                    if (!res.data.error) {
                        cart = [];
                        $scope.cart = cart
                    }
                    else {
                        alert(res.data);
                    }
                });
        };


        $scope.$on('UserLogin', function (event, data) {
            $scope.client = data;
        });

        $scope.$on('UserLogout', function (event, data) {
            cart = [];
            $scope.cart = cart
        });

        $scope.getClientInfo = function () {
            if (!$scope.client) {
                var req = {
                    method: "GET",
                    url: "/client"
                }
                $http(req)
                    .then(function (res) {
                        console.log(res.data.error)
                        if (!res.data.error) {
                            console.log("getLoginInfo");
                            console.log(res.data);
                            $scope.client = res.data;
                            if($scope.cart&&$scope.cart.length>0){
                                $('#myModal').modal("show");
                            }
                            else {
                                alert("购物车为空");
                            }
                        }
                        else {
                            alert("请先登录");
                        }
                    });
            }
            else {
                if($scope.cart&&$scope.cart.length>0){
                    $('#myModal').modal("show");
                }
                else {
                    alert("购物车为空");
                }
            }
        }

        $scope.$emit("changeTopStyle",false);
    })